﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using DTO;
using Business;

namespace QuanLyNhaSach
{
    public partial class ThemTheLoai : Form
    {

        //an hien button luu
        private void AnHienButtonSave()
        {
            if (dgvTheLoai.Rows.Count - 1 != 0)
            {
                btnSave.Enabled = true;
            }
            else
            {
                btnSave.Enabled = false;
            }
        }
        private void AnHienButtonXoa()
        {
            if (dgvTheLoai.Rows.Count - 1 != 0)
            {
              btnXoa.Enabled = true;
            }
            else
            {
                btnXoa.Enabled = false;
            }
        }
        public ThemTheLoai()
        {
            InitializeComponent();
        }
        private void SetUpDataGridViewTheLoai()
        {
            dgvTheLoai.BackgroundColor = Color.White;
            dgvTheLoai.ColumnCount = 2;
            dgvTheLoai.Columns[0].Name = "STT";
            dgvTheLoai.Columns[1].Name = "Tên Thể Loại";

            dgvTheLoai.Columns[0].Width = 80;
            dgvTheLoai.Columns[1].Width = 240;


            dgvTheLoai.Columns[0].ReadOnly = true;
            dgvTheLoai.MultiSelect = true;
            dgvTheLoai.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dgvTheLoai.DefaultCellStyle.SelectionBackColor = Color.Ivory; //Color.Silver;
            dgvTheLoai.DefaultCellStyle.SelectionForeColor = Color.Black;
                                               
        }
        private void UpdateSoThuTuDataGridViewTheLoai()
        {
            for (int i = 0; i < dgvTheLoai.Rows.GetRowCount(DataGridViewElementStates.Visible)-1 ; i++)
            {
                dgvTheLoai.Rows[i].Cells[0].Value = i + 1;
            }
        }

        private void QuanLyTheLoai_Load(object sender, EventArgs e)
        {
            SetUpDataGridViewTheLoai();
            MessageBox.Show(dgvTheLoai.Rows.Count.ToString());
            if (dgvTheLoai.Rows.Count - 1 == 0)
            {
                btnSave.Enabled = false;
                btnXoa.Enabled = false;
            }
            

        }

        private void btnClose_Click(object sender, EventArgs e)
        {

            this.Close();
            //QuanLyTheLoai_FormClosing();
        }

        private void QuanLyTheLoai_FormClosing(object sender, FormClosingEventArgs e)
        {
            if (btnSave.Enabled)
            {
                DialogResult r = MessageBox.Show("Bạn có muốn lưu dữ liệu không?", "Thông Báo", MessageBoxButtons.YesNo);
                if (r == DialogResult.Yes)
                {
                   btnSave_Click(sender,e);
                }
            }
         
        }

        private void dgvTheLoai_UserAddedRow(object sender, DataGridViewRowEventArgs e)
        {
            //for (int i = 0; i < dgvTheLoai.Rows.GetRowCount(DataGridViewElementStates.Displayed) - 1; i++)            
          //  MessageBox.Show("useraddedrow");
            UpdateSoThuTuDataGridViewTheLoai();
                                    
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            for (int i = 0; i < dgvTheLoai.Rows.GetRowCount(DataGridViewElementStates.Visible)-1; i++)
            {
                TheLoaiDTO theloaiDTO = new TheLoaiDTO();
                theloaiDTO.TenTheLoai = dgvTheLoai.Rows[i].Cells[1].Value.ToString();
                TheLoaiService.Insert(theloaiDTO);
             MessageBox.Show(dgvTheLoai.Rows[i].Cells[1].Value.ToString());
            }
            DialogResult r = MessageBox.Show("Thể Loại Sách Đã Lưu Thành Công", "Thông Báo", MessageBoxButtons.OK);
            if (r == DialogResult.OK)
            {
                dgvTheLoai.Rows.Clear();
            }

           
            
        }

        private void dgvTheLoai_RowsRemoved(object sender, DataGridViewRowsRemovedEventArgs e)
        {
           
            MessageBox.Show("rowsremoved");
          //  UpdateSoThuTuDataGridViewTheLoai();
            AnHienButtonSave();
            AnHienButtonXoa();
        }

        

        private void btnXoa_Click(object sender, EventArgs e)
        {
            if ( dgvTheLoai.SelectedRows[0].IsNewRow ==false)
             {
               
                int n = dgvTheLoai.Rows.IndexOf(dgvTheLoai.SelectedRows[0]);
                
                MessageBox.Show(n.ToString());
                  dgvTheLoai.Rows.Remove(dgvTheLoai.SelectedRows[0]);
             }
            
        }

        private void dgvTheLoai_RowsAdded(object sender, DataGridViewRowsAddedEventArgs e)
        {
            AnHienButtonSave();
            AnHienButtonXoa();
        }

       
       
      
    }
}
